package com.ruoyi.jksd.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.jksd.common.dto.XyBankinfoDto;
import com.ruoyi.jksd.domain.XyUsers;
import com.ruoyi.jksd.service.IXyUsersService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.jksd.mapper.XyBankinfoMapper;
import com.ruoyi.jksd.domain.XyBankinfo;
import com.ruoyi.jksd.service.IXyBankinfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员-银行卡信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-26
 */
@Service
public class XyBankinfoServiceImpl implements IXyBankinfoService {
    @Autowired
    private XyBankinfoMapper xyBankinfoMapper;

    @Autowired
    private IXyUsersService xyUsersService;

    /**
     * 查询会员-银行卡信息
     *
     * @param id 会员-银行卡信息主键
     * @return 会员-银行卡信息
     */
    @Override
    public XyBankinfo selectXyBankinfoById(String id) {
        return xyBankinfoMapper.selectXyBankinfoById(id);
    }

    /**
     * 查询会员-银行卡信息列表
     *
     * @param xyBankinfo 会员-银行卡信息
     * @return 会员-银行卡信息
     */
    @Override
    public List<XyBankinfo> selectXyBankinfoList(XyBankinfo xyBankinfo) {
        return xyBankinfoMapper.selectXyBankinfoList(xyBankinfo);
    }

    /**
     * 新增会员-银行卡信息
     *
     * @param xyBankinfo 会员-银行卡信息
     * @return 结果
     */
    @Override
    public int insertXyBankinfo(XyBankinfo xyBankinfo) {
        return xyBankinfoMapper.insertXyBankinfo(xyBankinfo);
    }

    /**
     * 修改会员-银行卡信息
     *
     * @param xyBankinfo 会员-银行卡信息
     * @return 结果
     */
    @Override
    public int updateXyBankinfo(XyBankinfo xyBankinfo) {
        return xyBankinfoMapper.updateXyBankinfo(xyBankinfo);
    }

    /**
     * 批量删除会员-银行卡信息
     *
     * @param ids 需要删除的会员-银行卡信息主键
     * @return 结果
     */
    @Override
    public int deleteXyBankinfoByIds(String[] ids) {
        return xyBankinfoMapper.deleteXyBankinfoByIds(ids);
    }

    /**
     * 删除会员-银行卡信息信息
     *
     * @param id 会员-银行卡信息主键
     * @return 结果
     */
    @Override
    public int deleteXyBankinfoById(String id) {
        return xyBankinfoMapper.deleteXyBankinfoById(id);
    }

    @Override
    public XyBankinfo selectXyBankinfoByUId(String uid) {
        return xyBankinfoMapper.selectXyBankinfoByUId(uid);
    }

    @Override
    @Transactional
    public AjaxResult editAddressAndpwd2(XyBankinfoDto xyBankinfoDto) {
        String xyUserId = xyBankinfoDto.getUid();
        XyUsers xyUsers = xyUsersService.selectXyUsersById(xyUserId);
        if (StringUtils.isNoneBlank(xyBankinfoDto.getPwd2())) {
            xyUsers.setPwd2(xyBankinfoDto.getPwd2());
            xyUsersService.updateXyUsers(xyUsers);
        }

        XyBankinfo xyBankinfo = xyBankinfoMapper.selectXyBankinfoByUId(xyUserId);

        if (StringUtils.isNotBlank(xyBankinfoDto.getCardnum())) {
            if (null == xyBankinfo) {
                xyBankinfo = new XyBankinfo();
                xyBankinfo.setBankname("USDT");
                xyBankinfo.setCardnum(xyBankinfoDto.getCardnum());
                xyBankinfo.setUid(xyUserId);
                xyBankinfo.setUsername(xyUsers.getUsername());
                xyBankinfo.setSite("USDT");
                xyBankinfo.setTel(xyUsers.getTel());
                xyBankinfo.setSite("1");
                xyBankinfoMapper.insertXyBankinfo(xyBankinfo);
            } else {
                xyBankinfo.setCardnum(xyBankinfoDto.getCardnum());
                xyBankinfoMapper.updateXyBankinfo(xyBankinfo);
            }

        }
        return AjaxResult.success();
    }
}
